﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace testing_thing
{
   public class FileWorldLoader : IWorldLoader
    {
        List<Location> loctemp = new List<Location>();
        public Location Load()
        {

            using (StreamReader sr = File.OpenText("swin-e-mon data.txt"))
            {
                #region Item file loading


                if (sr.ReadLine() != "// ITEMS")
                {
                    Console.WriteLine("File is corrupted(item)");
                }

                Inventory itmtemp = new Inventory();
                int noOfthings = System.Convert.ToInt32(sr.ReadLine());
                for (int i = 0; i < (noOfthings); i++)
                {
                    string id = sr.ReadLine();
                    string[] ids = id.ToLower().Split(',');
                    string title = sr.ReadLine();
                    string description = sr.ReadLine();
                    string stationary = sr.ReadLine();
                    string container = sr.ReadLine();
                    bool isContainer = false;
                    bool isStationary = false;

                    if (stationary == "Stationary")
                        isStationary = true;
                    if (container == "Container")
                        isContainer = true;

                    Item itm = new Item(ids, isContainer);
                    if (isContainer)
                    {
                        string cId = sr.ReadLine();
                        string[] cIds = cId.ToLower().Split(',');
                        for (int j = 0; j < cIds.Length; j++)
                        {
                            Item itmInContainer = itmtemp.TakeItem(cIds[j]);
                            if (itmInContainer != null)
                                itm.Inventory.AddItem(itmInContainer);
                        }
                    }
                    itm.Title = title;
                    itm.Description = description;
                    itm.IsStationary = isStationary;

                    itmtemp.AddItem(itm);

                }
                #endregion

                #region Location fileloading
                if (sr.ReadLine() != "// LOCATIONS")
                {
                    Console.WriteLine("File is corrupted(Location)");
                }


                int noOflocs = System.Convert.ToInt32(sr.ReadLine());
                for (int i = 0; i < (noOflocs); i++)
                {
                    string id = sr.ReadLine();
                    string[] ids = id.ToLower().Split(',');
                    string title = sr.ReadLine();
                    string description = sr.ReadLine();
                    string itemAtLoc = sr.ReadLine();
                    string[] itemsAtLoc = itemAtLoc.ToLower().Split(',');
                    Location loc = new Location(ids);
                    loc.Title = title;
                    loc.Description = description;
                    for (int k = 0; k < itemsAtLoc.Length; k++)
                    {
                        Item itemAddedToLoc = itmtemp.TakeItem(itemsAtLoc[k]);
                        if (itemAddedToLoc!=null)
                        loc.Inventory.AddItem(itemAddedToLoc);
                        else 
                            Console.WriteLine("you suck shit");
                    }
                    loctemp.Add(loc);


                }
                #endregion

                #region Path file loading
                if (sr.ReadLine() != "// PATHS")
                {
                    Console.WriteLine("File is corrupted(Paths)");
                }
                int noOfpths = System.Convert.ToInt32(sr.ReadLine());
                for (int i = 0; i < (noOfpths); i++)
                {
                    string id = sr.ReadLine();
                    string[] ids = id.ToLower().Split(',');
                    string title = sr.ReadLine();
                    string description = sr.ReadLine();
                    string fromToId = sr.ReadLine();
                    string[] fromToIds = fromToId.ToLower().Split(',');
                    Path pth = new Path(ids, accessLocation(fromToIds[1]));
                    pth.Title = title;
                    pth.Description = description;
                    Location locFrom = accessLocation(fromToIds[0]);
                    locFrom.AddPath(pth);
                }
                #endregion

                #region Start file loading
                if (sr.ReadLine() != "// START")
                {
                    Console.WriteLine("File is corrupted(Start)");
                }
                Location start = accessLocation(sr.ReadLine());
                return start;
                #endregion

            }
        }

        private Location accessLocation(string id)
        {
            foreach (Location l in loctemp)
            {
                if (l.IdentifiedBy(id))
                {
                    return l;
                }

            }
            return null;
        }
    }
}

